package com.chixing.service.impl;

import com.chixing.mapper.CarMapper;
import com.chixing.mapper.MyorderMapper;
import com.chixing.pojo.Car;
import com.chixing.pojo.Myorder;
import com.chixing.pojo.MyorderExample;
import com.chixing.service.IndexHotDateControllerService;
import com.chixing.util.ServerResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class IndexHotDateControllerServiceImpl implements IndexHotDateControllerService {

    @Autowired
    private CarMapper carMapper;

    @Autowired
    private MyorderMapper myorderMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public ServerResult getHotCar() {

        if (redisTemplate.hasKey("indexHotCar")) {
            System.out.println("该key存在");

            Set<Car> set = redisTemplate.opsForSet().members("indexHotCar");

            if (set != null)
                return ServerResult.getSussess(set);
        } else {



            Map<List<Myorder>, Integer> map = new HashMap<>();
            List<Car> carList = carMapper.selectByExample(null);
            System.out.println("carList" + carList);

            for (Car car : carList) {
                MyorderExample example = new MyorderExample();
                example.createCriteria().andCarIdEqualTo(car.getCarId());
                List<Myorder> myorderList = myorderMapper.selectByExample(example);
                map.put(myorderList, myorderList.size());
            }

            if (map.size() < 4) {
                List<Car> cars = carList.subList(0,4);
                System.out.println(cars);
                for (Car car:cars) {
                    redisTemplate.opsForSet().add("indexHotCar",car);
                    redisTemplate.expire("indexHotCar",30, TimeUnit.MINUTES);
                }
                Set<Car> set = redisTemplate.opsForSet().members("indexHotCar");

                if (set != null)
                    return ServerResult.getSussess(set);
            }

            //Map排序
            List<List<Myorder>> mobileList = map.entrySet().stream()
                    .sorted((Map.Entry<List<Myorder>, Integer> o1, Map.Entry<List<Myorder>, Integer> o2) -> o2.getValue() - o1.getValue())
                    .map(entry -> entry.getKey()).collect(Collectors.toList())
                    .subList(0, 4);

            for (int i = 0; i < mobileList.size(); i++) {

                System.out.println(mobileList.get(i).get(0).getCarId());

                Car car = carMapper.selectByPrimaryKey(mobileList.get(i).get(0).getCarId());

                redisTemplate.opsForSet().add("indexHotCar",car);
            }
            //设置生存周期
            redisTemplate.expire("indexHotCar",30, TimeUnit.MINUTES);

            Set<Car> set = redisTemplate.opsForSet().members("indexHotCar");

            if (set != null)
                return ServerResult.getSussess(set);
        }
        return ServerResult.getFail(null);
    }

}
